package org.bunny.vehicle.service;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.bunny.vehicle.entity.User;
import org.bunny.vehicle.util.Const;
import org.springframework.stereotype.Component;

/**
 * 服务切面
 * 
 * @author Bunny
 * @version 2013-6-12 上午11:18:27
 * 
 */
@Component
@Aspect
public class ServiceAspect extends BaseService {

	/**
	 * 初始化状态
	 */
	private static boolean initialized = false;

	@Pointcut("execution (* org.bunny.vehicle.service.*.*(..))")
	public void serviceCall() {
	}

	@Pointcut("execution (* org.bunny.vehicle.service.*.*.*(..))")
	public void adminServiceCall() {
	}

	@Before("(serviceCall() || adminServiceCall()) && args(json)")
	public void beforeServiceCall(JoinPoint joinPoint, String json) {
		// 服务调用日志
		log.info(joinPoint.toString() + json);

		// 初始化管理员
		if (!initialized && null == userRepository.findByNumber(Const.ADMIN_NUMBER)) {
			Date date = new Date();
			User admin = new User();
			admin.setNumber(Const.ADMIN_NUMBER);
			admin.setPassword(Const.ADMIN_PASSWORD);
			admin.setName(Const.ADMIN_NUMBER);
			admin.setClassification(User.CLASS_ADMIN);
			admin.setBirthday(date);
			admin.setJoinDate(date);
			userRepository.save(admin);
		}
	}

}
